using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Net.Fan.SmallPig.DataBase;


namespace Net.Fan.SmallPig.DataAccess
{
    /// <summary>
    /// 
    /// </summary>
    public class PointDA
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyName"></param>
        /// <returns></returns>
        public static long GetCompanyPoint(string companyName)
        {
            string sql = "select Point from CompanyPoint where companyName = @companyName";

            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@companyName", companyName);

            return (long)DA_Base.ExecuteScalar(sql, paras);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public static int MinusCompanyPoint(string companyName, long point)
        {
            string sql = "Update CompanyPoint set Point=Point-@Point where companyName = @companyName";

            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@companyName", companyName);
            paras[1] = new SqlParameter("@Point", point);
            return DA_Base.ExecuteNonQuery(sql, paras);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="point"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public static int ChargeCompanyPoint(string companyName, long point, string remark)
        {
            string sql = "Update CompanyPoint set Point=Point+@Point where companyName = @companyName";

            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@companyName", companyName);
            paras[1] = new SqlParameter("@Point", point);
            DA_Base.ExecuteNonQuery(sql, paras);

            sql = "insert ComPointLog (CompanyName,Point,Remark,LogTime) values ( @CompanyName,@Point,@Remark,GetDate())";

            paras = new SqlParameter[3];
            paras[0] = new SqlParameter("@CompanyName", companyName);
            paras[1] = new SqlParameter("@Point", point);
            paras[2] = new SqlParameter("@Remark", remark);

            return DA_Base.ExecuteNonQuery(sql, paras);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public static bool CreateCompanyPointRecord(string companyName, long point)
        {
            string sql = "Insert CompanyPoint (companyName,Point) values (@CompanyName,@Point)";

            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@companyName", companyName);
            paras[1] = new SqlParameter("@Point", point);
            if (DA_Base.ExecuteNonQuery(sql, paras) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyName"></param>
        /// <returns></returns>
        public static bool DeleteCompanyPointRecord(string companyName)
        {
            string sql = "Delete CompanyPoint where CompanyName=@CompanyName";

            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@companyName", companyName);

            if (DA_Base.ExecuteNonQuery(sql, paras) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyName"></param>
        /// <returns></returns>
        public static DataTable GetCompanyPointLog(string companyName)
        {
            string sql = "Select * from ComPointLog where CompanyName = @CompanyName order by LogTime desc";

            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@CompanyName", companyName);
            return DA_Base.RunReturnDataset(sql, paras).Tables[0];
        }
    }
}
